<?php
namespace app\api\controller\v1;

use Throwable;
use ba\Captcha;
use ba\ClickCaptcha;
use think\facade\Config;
use app\common\facade\Token;
use thinkct\controller\Frontend;
use app\api\validate\User as UserValidate;

class User extends Frontend
{
    protected array $noNeedLogin = ['login', 'register', 'logout'];

    protected array $noNeedPermission = ['index'];

    public function initialize(): void
    {
        parent::initialize();
    }

    /**
     * 用户登录
     * @throws Throwable
     */
    public function login(): void
    {
        $openMemberCenter = Config::get('buildadmin.open_member_center');
        if (!$openMemberCenter) {
            $this->error(__('Member center disabled'));
        }

        // 检查登录态
        if ($this->auth->isLogin()) {
            $this->success(__('You have already logged in. There is no need to log in again~'), [
                'routePath' => '/user'
            ], 302);
        }

        if ($this->request->isPost()) {
            $params = $this->request->post(['username', 'password', 'keep', 'captcha', 'captchaId', 'captchaInfo']);
            $res = $this->auth->login($params['username'], $params['password'], (bool)$params['keep']);
            if ($res === true) {
                $this->success(__('Login succeeded!'), [
                    'userInfo'  => $this->auth->getUserInfo(),
                    'routePath' => '/user'
                ]);
            } else {
                $msg = $this->auth->getError();
                $msg = $msg ?: __('Login failed, please try again or contact the website administrator~');
                $this->error($msg);
            }
        }
    }

    /**
     * 用户注册
     * @throws Throwable
     */
    public function register(): void
    {
        $openMemberCenter = Config::get('buildadmin.open_member_center');
        if (!$openMemberCenter) {
            $this->error(__('Member center disabled'));
        }

        // 检查登录态
        if ($this->auth->isLogin()) {
            $this->success(__('You have already logged in. There is no need to log in again~'), [
                'routePath' => '/user'
            ], 302);
        }

        // 判断是否注册验证
//            if(get_sys_config('user_register_verify')) {
//                $captcha = $this->request->post('captcha');
//                $registerType = $this->request->post('registerType');
//                $email = $this->request->post('email');
//                $mobile = $this->request->post('mobile');
//                if (!$captcha) {
//                    $this->error('请输入验证码');
//                }
//                $captchaObj = new Captcha();
//                if (!$captchaObj->check($captcha, ($registerType == 'email' ? $email : $mobile) . 'user_register')) {
//                    $this->error(__('Please enter the correct verification code'));
//                }
//            }

        if ($this->request->isPost()) {
            $params = $this->request->post(['username', 'password', 'mobile', 'email', 'captcha', 'registerType']);
            $res = $this->auth->register($params['username'], $params['password'], $params['mobile'], $params['email']);
            if ($res === true) {
                $this->success(__('Registration succeeded!'), [
                    'userInfo'  => $this->auth->getUserInfo(),
                    'routePath' => '/user'
                ]);
            } else {
                $msg = $this->auth->getError();
                $msg = $msg ?: __('Registration failed, please try again or contact the website administrator~');
                $this->error($msg);
            }
        }
    }

    /**
     * 用户退出
     * @throws Throwable
     */
    public function logout(): void
    {
        if ($this->request->isPost()) {
            $refreshToken = $this->request->post('refreshToken', '');
            if ($refreshToken) Token::delete((string)$refreshToken);
            $this->auth->logout();
            $this->success();
        }
    }
}